
var mode = "";

$(function(){
    mode = $("input[name=mode]").val();
    $additem = $("#js-additem");
    $cadditem = $("#js-additem");

    // init some date time picker control
    $(".jk-datetimepicker").datetimepicker(
        {
            autoclose:1,
        }
    );

    $cadditem.click(function(){
        if ($additem.css("display") == "none") {
        	$additem.show("slow");
            jk_open_tips("#item-content");
        } else {
        	// $additem.hide("slow");
            $additem.remove("display").addClass('no-display');
            jk_close_tips("#item-content");
        }
        $("#item-content .js-close").on('click', function(){
            $additem.remove("display").addClass('no-display');
            jk_close_tips("#item-content");
        });
    });
    $additem.find(".ownsure").click(function(event){

        var name = $additem.find("input[name=name]").val().trim();
        var content = $additem.find("div[name=content]").html().trim();
        var classify = $additem.find("input[name=classify]").val().trim();
        content = encodeURIComponent(content);

        var len = name.length;
        if (len <= 0) {
            alert("怎么说也要写个名称吧");
            return;
        }
        len = content.length;
        if (len <= 0) {
            alert("写点说明再提交哦^-^");
            return;
        }
        len = classify.length;
        if (len <= 0) {
            alert("还望指明平台");
            return;
        }

        var string = "ajax=&name=" + name + "&classify=" + classify + "&content=" + content + "&mode=" + mode;
        // owndug has startime and stoptime
        if (mode == "ownbug") {
            var starttime = $additem.find("input[name=starttime]").val();
            var stoptime = $additem.find("input[name=stoptime]").val();
            var operate = $additem.find("input[name=operate]").val();
            // alert("starttime: "+starttime+",stoptime="+stoptime);
            string+="&starttime="+starttime+"&stoptime="+stoptime+"&operate="+operate;
        }
        $.ajax({
            url: "OwnAddItem.html",
            type: "post",
            dataType: "json",
            data: string,
            complete: responseAdd
        });
    });

    // #additem #sure
    $("#sure").click(function() {
        var classify = $additem.find("input[name=classify]").val().trim();
        var content = $additem.find("div[name=content]").html();
        content = encodeURIComponent(content);

        var len = classify.length;
        if (len <= 0) {
            alert("怎么说也要写个固件名称吧");
            return;
        }
        len = content.length;
        if (len <= 0) {
            alert("写点说明再提交哦^-^");
            return;
        }

//         alert(classify + "," + content + "," + mode);
        $.ajax({
        	url: "AddItem.html",
        	type: "post",
        	dataType: "json",
        	data: "ajax=&classify=" + classify + "&content=" + content + "&mode=" + mode,
        	complete: responseAdd
        });
    });

    $("button.mod").bind('click', modfunc);

    $('button.del').click(function(ev){
        var confirm_content = "<h4>If You want delete, give me the code</h4>" + 
                              "<div><input type='password' class='input-text input-small input-danger' name='code' placeholder='you, TELL ME CODE' /></div>";
        var classify = $(this).parent().siblings().children('div input[name=classify]').val().trim();
        var name = $(this).parent().siblings().children('input[name=name]').val();
        if (name != undefined) {
            name.trim();
        }
        var string = "ajax=&classify=" + classify + "&mode=" + mode;
        if (name != undefined) string += "&name=" + name;
        var delTips = new JKTips(confirm_content, 200, 100, doDel);
        delTips.string = string;
        confirmWindow(ev, delTips);
    });

    $('#search').click(function(){

        var classify = $(this).siblings('input').val().trim();
        var len = classify.length;
        if (len <= 0) {
            alert("什么都木有，我没法找啊");
            return;
        }
        var string = "ajax=";
        if (mode == "ownbug") {
            string += "&name=" + classify + "&mode=" + mode;
        } else {
            string += "&classify=" + classify + "&mode=" + mode;
        }
        // alert(string);

        $.ajax({
            url: "SearchItem.html",
            type: "post",
            dataType: "json",
            data: string,
            complete: responseSearch
        });
    });
});

function doDel (string) {
    jk_send_request_post("DelItem.html", string, responseAdd);
}

function doMod (string) {
    jk_send_request_post("ModItem.html", string, responseAdd);
}

function responseAdd(data, textStatus) {
    var result = dealSuccess(data, textStatus);
    if (result != true && result != false) {
        alert(result);
    }
}

var modfunc = function() {
        var divtext = $(this).parent().siblings().children('div[name=content]');
        setDivEditable(divtext);
        $(this).before("<button class='btn modok' >提交</button>");

        var string = "ajax=";
        var name = $(this).parent().siblings().children('input[name=name]').val();
        if (name != undefined) {
            name.trim();
            string += "&name="+ name;
        }
        
        var classify = $(this).parent().siblings().children('input[name=classify]').val().trim();
        string += "&classify=" + classify;  

        string += "&mode=" + mode;

        $(this).remove();

        $("button.modok").click(function(ev){
            // $(this).removeClass('modok').addClass('mod').text("修改");
            $(this).before("<button class='btn btn-warn mod' >修改</button>");
            $("button.mod").bind('click', modfunc);

            setDivUneditable(divtext);

            var fixedcheckbox = $(this).parent().siblings().children('input[name=fixed]');
            var fixed = null;
            if (fixedcheckbox != undefined) {
                if (fixedcheckbox.attr("value") == "fixed") {

                fixed = fixedcheckbox.is(":checked");
                if (fixed == true) {
                    string += "&fixed=" + fixed;
                }
                } else {
                    string += "&fixed=" + true;
                }
            }

            var content = $(this).parent().siblings().children('div[name=content]').html();

            content = encodeURIComponent(content);
            string += "&content=" + content;
            if (mode == "ownbug") {
                var operate = $(this).parent().siblings().children('input[name=operate]').val().trim();
                string += "&operate=" + operate;
            }

            var confirm_content = "<h4>If You want mod, give me the code</h4>" + 
                              "<div><input type='text' class='input-text input-small input-danger' name='code' placeholder='you, TELL ME CODE' /></div>";
            var modTips = new JKTips(confirm_content, 200, 100, doMod);
            modTips.string = string;
            confirmWindow(ev, modTips);
            $(this).remove();
        });
    }

function responseSearch(data, textStatus) {
    if (textStatus == "success") {
        result = $.parseJSON(data.responseText);
        if (result.status == -1) {
            alert("啊哦，我什么也没找到");
            return;
        } else {
            var fixed ="";
            if (result.Fixed == true) {
                fixed = "<div name='upgradetime'> </div>";
            } else {
                fixed = "Unfixed";
            }
            $('div.search-result').removeClass('no-display');
            $('div.search-result ul.result').remove();
            obj = "<tr class='result'>" +
                  "<td><input type='text' class='input-text input-middle' name='name' value='" + result.Name + "' disabled=true/></td>" +
                  "<td><div class='jk-input-text-none'>" + result.Content + "</div></td>" +
                  "<td><input type='text' class='input-text input-small' name='classify' value='" + result.Classify + "' disabled=true /></td>" +
                  "<td><div name='createtime' ></div></td>" + 
                  "<td>" + fixed + "</td></tr>";
            $('div.search-result tbody').append(obj);
        }
    }
}

